<?php
    require_once(APPPATH . '/controllers/test/EXT_Toast.php');

    class objects_test extends EXT_Toast
    {

        var $object_id = 164;
        var $partner_id = 12;
        var $destination_id = 1;

        function __construct()
        {
            parent::__construct(__FILE__);            
            //Check if model exists and load
            $model = $this->model_exists('objects_model');
            if($model) $this->load->model('objects_model');
        }

         function test_insert_hotel()
        {

            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'insert');
            if(!$method)return;

            //Post data sent by view
            $post = array(
                'post_action' => 'NEW',
                'object_type_ID' =>1,
                'object_destination' =>$this->destination_id,
                'object_partner' =>$this->partner_id,
                'name' => 'apartman',
                'number_of_stars' => '2',
                'distance_from_the_sea' =>'400m',
                'distance_from_the_center' =>'1km',
                'contact_phone' =>'030312314',
                'contact_fax' =>'030312314',
                'address' =>'Popa jola zeca bb',
                'search_priority' =>'5',
                'infant_age' =>'10',
                'contact_phone_reception' =>'030312314',
                'contact_email' =>'mail@apartman.me',
                'web_page' =>'www.apartman.me'
            );

            // DO SOME DATA ARRANGEMENT
            $object_type_ID = $post['object_type_ID'];
            $destinations_ID = $post['object_destination'];
            $partners_ID = $post['object_partner'];
            $post_action = $post['post_action'];
            unset($post['object_type_ID']);
            unset($post['object_destination']);
            unset($post['object_partner']);
            unset($post['post_action']);
            if($post_action=='EDIT'):
                $object_ID = $post['object_ID'];
                unset($post['object_ID']); 
                endif;

            $data = array(
                'destinations_ID' => $destinations_ID,
                'partners_ID' => $partners_ID,
                'name' => $post['name'],
                'status' => 1,
                'date_time' => time(),
                'fields' => serialize($post),
            );

            switch ($post_action) {
                case "EDIT":
                    $this->objects_model->update($object_ID,$data);
                    $success = ($this->db->affected_rows()>=0);
                    break;
                case "NEW":
                    $data['object_type_ID'] = $object_type_ID;
                    $insert_id = $this->objects_model->insert($data);
                    $success = $insert_id;
            }

            if($success)
            {
                $this->_assert_true(TRUE);
            }else
            {
                $this->_fail('This should not heppend :)');
            }
        }
        
        function test_view_hotels_entries()
        {
            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'list_all');
            if(!$method)return;

            //Check results from objects list_all
            $objects = $this->objects_model->list_all();
            $count_is_greater_than_zero = (count($objects) > 0);
            $this->_assert_true($count_is_greater_than_zero);
            $this->_assert_true(is_array($objects));
            foreach($objects as $result) {  
                $this->_assert_true(is_array($result));
                $this->_assert_true(isset($result['object_ID']));  
                $this->_assert_true(isset($result['object_name']));  
                $this->_assert_true(isset($result['partner_ID']));  
                $this->_assert_true(isset($result['partner_name']));  
                $this->_assert_true(isset($result['destination_ID']));  
                $this->_assert_true(isset($result['destination_name']));  
                $this->_assert_true(isset($result['object_type_ID']));  
                $this->_assert_true(isset($result['object_type_type']));
            }  
        }

        function view_hotels_with_no_entries()
        {
            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'list_all');
            if(!$method)return;

            //Check results from objects list_all
            $objects = $this->objects_model->list_all();
            $this->_assert_equals($objects, array());  
        }

        function test_view_hotel_entry()
        {
            /* 
            * GET OBJECT
            */

            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'list_by_id');
            if(!$method)return;

            //Check results from objects list_all
            $object = $this->objects_model->list_by_id($this->object_id);
            $count_is_greater_than_zero = (count($object) > 0);
            $this->_assert_true($count_is_greater_than_zero);
            $this->_assert_true(is_array($object));

            $this->_assert_true(isset($object['object_ID']));  
            $this->_assert_true(isset($object['object_name']));  
            //$this->_assert_true(isset($object['object_fields']));  
            $this->_assert_true(isset($object['partner_ID']));  
            $this->_assert_true(isset($object['partner_name']));  
            $this->_assert_true(isset($object['destination_ID']));  
            $this->_assert_true(isset($object['destination_name']));  
            $this->_assert_true(isset($object['object_type_ID']));  
            $this->_assert_true(isset($object['object_type_type']));

            /* 
            * GET GENERAL ID ON OBJECT
            */

            //Check if model exists and load
            $model = $this->model_exists('object_groups_model');
            if($model) $this->load->model('object_groups_model');

            //Check if method exists
            $method = $this->func_exists($this->object_groups_model, 'find_general_group');
            if(!$method)return;

            //Check results from object_groups find_general_group
            $object_groups = $this->object_groups_model->find_general_group($object['object_type_ID']);            
            $object_groups_count = (count($object_groups) > 0);
            $this->_assert_true($object_groups_count);
            $this->_assert_true(is_array($object_groups));
            $this->_assert_true(isset($object_groups['object_type_ID']));  
            $this->_assert_true(isset($object_groups['group_ID']));
            $this->_assert_true(isset($object_groups['group_name']));

            /* 
            * GET ATTRIBUTES ON GENERAL GROUP
            */

            //Check if model exists and load
            $model = $this->model_exists('group_attributes_model');
            if($model) $this->load->model('group_attributes_model');

            //Check if method exists
            $method = $this->func_exists($this->group_attributes_model, 'get_attributes_on_group');
            if(!$method)return;

            //Check results from group_attributes get_attributes_on_group
            $general_attributes = $this->group_attributes_model->get_attributes_on_group($object_groups['group_ID']);
            $count_general_attributes = (count($general_attributes) > 0);
            $this->_assert_true($count_general_attributes);
            $this->_assert_true(is_array($general_attributes));
            foreach($general_attributes as $attribute) {  
                $this->_assert_true(is_array($attribute));
                $this->_assert_true(isset($attribute['attribute_ID']));  
                $this->_assert_true(isset($attribute['attribute_name']));  
                $this->_assert_true(isset($attribute['attribute_type']));  
                $this->_assert_true(isset($attribute['attribute_required']));  
                $this->_assert_true(isset($attribute['attribute_custom']));  
                $this->_assert_true(isset($attribute['attribute_sort']));  
            }  

            /* 
            * GET HTML ON GENERAL GROUP
            */

            //Check if model exists and load
            $model = $this->model_exists('groups_model');
            if($model) $this->load->model('groups_model');

            //Check if method exists
            $method = $this->func_exists($this->groups_model, 'get_html_on_group');
            if(!$method)return;

            //Check results from groups_model get_html_on_group
            $this->load->helper('inflector');
            $this->load->helper('component');
            $general_html_edit = $this->groups_model->get_html_on_group($general_attributes,$object['object_fields']);
            $general_html_new = $this->groups_model->get_html_on_group($general_attributes);

            //$this->firephp->log($general_html_edit);
            /*$this->firephp->log($general_html_new);*/
        }

        function test_edit_hotel()
        {
            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'update');
            if(!$method)return;

            //Post data sent by view
            $post = array(
                'post_action' => 'EDIT',
                'object_ID' =>$this->object_id,
                'object_type_ID' =>1,
                'object_destination' =>$this->destination_id,
                'object_partner' =>$this->partner_id,
                'name' => 'apartman',
                'number_of_stars' => '2',
                'distance_from_the_sea' =>'400m',
                'distance_from_the_center' =>'1km',
                'contact_phone' =>'030312314',
                'contact_fax' =>'030312314',
                'address' =>'Popa jola zeca bb',
                'search_priority' =>'5',
                'infant_age' =>'10',
                'contact_phone_reception' =>'030312314',
                'contact_email' =>'mail@apartman.me',
                'web_page' =>'www.apartman.me'
            );

            // DO SOME DATA ARRANGEMENT
            $object_type_ID = $post['object_type_ID'];
            $destinations_ID = $post['object_destination'];
            $partners_ID = $post['object_partner'];
            $post_action = $post['post_action'];
            unset($post['object_type_ID']);
            unset($post['object_destination']);
            unset($post['object_partner']);
            unset($post['post_action']);
            if($post_action=='EDIT'):
                $object_ID = $post['object_ID'];
                unset($post['object_ID']); 
                endif;

            /* 
            * GET OBJECT
            */

            //Check results from objects list_all
            $object = $this->objects_model->list_by_id($this->object_id);
            $this->_assert_true(count($object) > 0);
            $this->_assert_true(is_array($object));

            $this->_assert_true(isset($object['object_fields']));                

            /* 
            * GET GENERAL ID ON OBJECT
            */

            //Check results from object_groups find_general_group
            $object_groups = $this->object_groups_model->find_general_group($object_type_ID);            
            $object_groups_count = (count($object_groups) > 0);
            $this->_assert_true($object_groups_count);
            $this->_assert_true(is_array($object_groups));
            $this->_assert_true(isset($object_groups['object_type_ID']));  
            $this->_assert_true(isset($object_groups['group_ID']));
            $this->_assert_true(isset($object_groups['group_name']));

            /* 
            * GET ATTRIBUTES ON GENERAL GROUP
            */

            // Get fields from object as array
            $fields = unserialize($object['object_fields']);
            $this->load->helper('inflector');
            $this->load->helper('component');

            $general_attributes = $this->group_attributes_model->get_attributes_on_group($object_groups['group_ID']);
            $count_general_attributes = (count($general_attributes) > 0);
            $this->_assert_true($count_general_attributes);
            $this->_assert_true(is_array($general_attributes));
            foreach($general_attributes as $attribute) {  

                $attribute_name = format_input_name($attribute['attribute_name']);
                //$this->firephp->log($object_group['group_name'].": ".$attribute['attribute_name'].": ".$attribute_name);
                $fields[$attribute_name] = $post[$attribute_name];
            }        

            $data = array(
                'destinations_ID' => $destinations_ID,
                'partners_ID' => $partners_ID,
                'name' => $post['name'],
                'status' => 1,
                'date_time' => time(),
                'fields' => serialize($fields),
            );

            switch ($post_action) {
                case "EDIT":
                    $this->objects_model->update($object_ID,$data);
                    $success = ($this->db->affected_rows()>=0);
                    break;
                case "NEW":
                    $data['object_type_ID'] = $object_type_ID;
                    $insert_id = $this->objects_model->insert($data);
                    $success = $insert_id;
            }

            if($success)
            {
                $this->_assert_true(TRUE);
            }else
            {
                $this->_fail('This should not heppend :)');
            }

        }       

        function test_view_hotel_description_groups()
        {
            /* 
            * GET OBJECT
            */

            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'list_by_id');
            if(!$method)return;

            //Check results from objects list_all
            $object = $this->objects_model->list_by_id($this->object_id);
            $count_is_greater_than_zero = (count($object) > 0);
            $this->_assert_true($count_is_greater_than_zero);
            $this->_assert_true(is_array($object));

            $this->_assert_true(isset($object['object_ID']));  
            $this->_assert_true(isset($object['object_name']));  
            //$this->_assert_true(isset($object['object_fields']));  
            $this->_assert_true(isset($object['partner_ID']));  
            $this->_assert_true(isset($object['partner_name']));  
            $this->_assert_true(isset($object['destination_ID']));  
            $this->_assert_true(isset($object['destination_name']));  
            $this->_assert_true(isset($object['object_type_ID']));  
            $this->_assert_true(isset($object['object_type_type']));

            /* 
            * GET DESCRIPTION GROUPS ON OBJECT
            */

            //Check results from object_groups find_general_group
            $object_groups = $this->object_groups_model->find_descriptions_groups($object['object_type_ID']);            
            $object_groups_count = (count($object_groups) > 0);
            $this->_assert_true($object_groups_count);
            $this->_assert_true(is_array($object_groups));
            foreach($object_groups as $object_group) { 
                $this->_assert_true(isset($object_group['object_type_ID']));  
                $this->_assert_true(isset($object_group['group_ID']));
                $this->_assert_true(isset($object_group['group_name']));
                /*$this->firephp->log($object_group['object_type_ID']);
                $this->firephp->log($object_group['group_ID']);
                $this->firephp->log($object_group['group_name']);*/
            }

            /* 
            * GET ATTRIBUTES ON DESCRIPTION GROUPS
            */

            $this->load->helper('inflector');
            $this->load->helper('component');
            foreach($object_groups as $object_group) { 
                //Check results from group_attributes get_attributes_on_group
                $group_attributes = $this->group_attributes_model->get_attributes_on_group($object_group['group_ID']);
                $count_general_attributes = (count($group_attributes) > 0);
                $this->_assert_true($count_general_attributes);
                $this->_assert_true(is_array($group_attributes));

                // For preview list all attributes in group
                foreach($group_attributes as $attribute) {  
                    $this->_assert_true(is_array($attribute));
                    $this->_assert_true(isset($attribute['attribute_ID']));  
                    $this->_assert_true(isset($attribute['attribute_name']));  
                    $this->_assert_true(isset($attribute['attribute_type']));  
                    $this->_assert_true(isset($attribute['attribute_required']));  
                    $this->_assert_true(isset($attribute['attribute_custom']));  
                    $this->_assert_true(isset($attribute['attribute_sort']));  

                    $attribute_name = format_input_name($attribute['attribute_name']);

                    //$this->firephp->log($object_group['group_name'].": ".$attribute['attribute_name'].": ".$attribute_name);
                }  
            }

            /* 
            * GET HTML ON DESCRIPTION GROUPS
            */

            //Check results from groups_model get_html_on_group
            $this->load->helper('inflector');
            $this->load->helper('component');
            foreach($object_groups as $object_group) {

                //Check results from group_attributes get_attributes_on_group
                $group_attributes = $this->group_attributes_model->get_attributes_on_group($object_group['group_ID']);
                $count_general_attributes = (count($group_attributes) > 0);
                $this->_assert_true($count_general_attributes);
                $this->_assert_true(is_array($group_attributes));

                $general_html_edit = $this->groups_model->get_html_on_group($group_attributes,$object['object_fields']);
                $general_html_new = $this->groups_model->get_html_on_group($group_attributes);

                //$this->firephp->log($object_group['group_name'].": ".$general_html_edit);

            }

        }

        function test_insert_description()
        {
            //Check if method exists
            $method = $this->func_exists($this->objects_model, 'update');
            if(!$method)return;

            //Post data sent by view
            $post = array(
                'post_action' =>'EDIT',
                'object_ID' => $this->object_id,
                'airport' => 'test data',
                'ambulance' => 'test data',
                'bank_office' => 'test data',
                'bus_station' => 'test data',
                'bus_station_is' => 'test data',
                'distance_from_the_center' => 'test data',
                'distance_from_the_sea' => 'test data',
                'exchange_offices' => 'test data',
                'farmacies' => 'test data',
                'ferry_port' => 'test data',
                'gas_stations' => 'test data',
                'hospital' => 'test data',
                'main_roads' => 'test data',
                'marketplace' => 'test data',
                'post_office' => 'test data',
                'railway_station' => 'test data',
                'restaurant' => 'test data',
                'beach' => 'test data',
                'shop' => 'test data',
                'tennis_courts' => 'test data',
                'tourist_agency' => 'test data',
                'hotel_beach' => 'test data',
                'pebble_beach' => 'test data',
                'rocky_beach' => 'test data',
                'sandy_beach' => 'test data',
                'authentic_accommodation' => 'test data',
                'barbecue' => 'test data',
                'berth' => 'test data',
                'children_playground' => 'test data',
                'childrens_pool' => 'test data',
                'floor' => 'test data',
                'free_access_for_vehicles' => 'test data',
                'indoor_pool' => 'test data',
                'outdoor_pool' => 'test data',
                'owner_does_not_live_in_object' => 'test data',
                'park' => 'test data',
                'parking_place_in_yard' => 'test data',
                'parking_space_on_public_road' => 'test data',
                'sauna' => 'test data',
                'shared_garden' => 'test data',
                'the_owner_lives_in_the_same_building' => 'test data',
                'washing_machine_at_landlords_against_fee' => 'test data',
                'x_coordinate' => 'test data',
                'y_coordinate' => 'test data'
            );

            // DO SOME DATA ARRANGEMENT
            $object_ID = $post['object_ID'];
            unset($post['object_ID']); 
            unset($post['post_action']); 


            /* 
            * GET OBJECT
            */

            //Check results from objects list_all
            $object = $this->objects_model->list_by_id($object_ID);
            $count_is_greater_than_zero = (count($object) > 0);
            $this->_assert_true($count_is_greater_than_zero);
            $this->_assert_true(is_array($object));

            $this->_assert_true(isset($object['object_ID']));  
            $this->_assert_true(isset($object['object_name']));  
            $this->_assert_true(isset($object['object_fields']));  
            $this->_assert_true(isset($object['partner_ID']));  
            $this->_assert_true(isset($object['partner_name']));  
            $this->_assert_true(isset($object['destination_ID']));  
            $this->_assert_true(isset($object['destination_name']));  
            $this->_assert_true(isset($object['object_type_ID']));  
            $this->_assert_true(isset($object['object_type_type']));


            /* 
            * GET DESCRIPTION GROUPS ON OBJECT
            */

            //Check results from object_groups find_general_group
            $object_groups = $this->object_groups_model->find_descriptions_groups($object['object_type_ID']);            
            $object_groups_count = (count($object_groups) > 0);
            $this->_assert_true($object_groups_count);
            $this->_assert_true(is_array($object_groups));

            /* 
            * GET ATTRIBUTES ON DESCRIPTION GROUPS
            */

            // Get fields from object as array
            $fields = unserialize($object['object_fields']);
            $this->load->helper('inflector');
            $this->load->helper('component');

            foreach($object_groups as $object_group) { 
                //Check results from group_attributes get_attributes_on_group
                $group_attributes = $this->group_attributes_model->get_attributes_on_group($object_group['group_ID']);
                $count_group_attributes = (count($group_attributes) > 0);
                $this->_assert_true($group_attributes);
                $this->_assert_true(is_array($group_attributes));

                // For preview list all attributes in group
                foreach($group_attributes as $attribute) {  

                    $attribute_name = format_input_name($attribute['attribute_name']);
                    //$this->firephp->log($object_group['group_name'].": ".$attribute['attribute_name'].": ".$attribute_name);
                    $fields[$attribute_name] = $post[$attribute_name];
                } 

            }

            $data = array(
                'fields' => serialize($fields)
            );

            $this->objects_model->update($object_ID,$data);

            if($this->db->affected_rows()>=0)
            {
                $this->_assert_true(TRUE);
            }else
            {
                $this->_fail('This should not heppend :)');
            }
        }

    }
?>
